package com.briup.mt.service.impl;

import com.briup.mt.service.IGreenhouseService;
import com.briup.mt.bean.Greenhouse;
import com.briup.mt.bean.GreenhouseExample;
import com.briup.mt.dao.GreenhouseMapper;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class GreenhouseServiceImpl implements IGreenhouseService {
    @Resource
    private GreenhouseMapper greenhouseMapper;

    @Override
    public List<Greenhouse> query(String name) {
        GreenhouseExample example = new GreenhouseExample();
        if (!StringUtils.isEmpty(name)) {
            example.createCriteria().andNameLike("%" + name + "%");
        }
        List<Greenhouse> list = greenhouseMapper.selectByExample(example);
        return list;
    }

    @Override
    public void saveOrUpdate(Greenhouse greenhouse) {
        if(greenhouse.getId()==null){
            greenhouse.setCreateTime(new Date().getTime());
            greenhouseMapper.insert(greenhouse);
        }else{
            greenhouseMapper.updateByPrimaryKey(greenhouse);
        }
    }

    @Override
    public void deleteById(long id) {
        greenhouseMapper.deleteByPrimaryKey(id);
    }
}
